const {
  $util,
} = getApp();
Page({
  data: {
    selectnum: 0, // 选择数量
    optionid: [], // 投票选项id
    bgColor: ['#FF0026', '#FF9500', '#FFCC00','#27BF76','#32C5FF','#4185F4','#5856D6','#FF2D55'],
    imgUrl: $util.getImgUrl(),
  },
  onLoad(opts) {
    this.setData(opts);
    this.getVoteInfo(opts.id);
    wx.setNavigationBarTitle({
      title: '投票详情',
    });
  },
  // 获取投票信息
  getVoteInfo(id) {
    $util.request({
      url: 'entry/wxapp/partyman.vote.getVoteOption',
      data: {
        voteid: id,
      },
      success: (res) => {
        this.setData({
          option: res.list,
          info: res.voteinfo,
        });
      },
    });
  },
  submit() {
    const {
      info,
      selectnum,
      id,
      isresult,
    } = this.data;
    let {
      optionid,
    } = this.data;
    const maxselect = info.maxselect;
    if (maxselect === '1' && selectnum === 0) {
      wx.showToast({
        title: `请选择一项进行投票`,
        icon: 'none',
      });
      return;
    }
    if (maxselect !== '1') {
      if (selectnum === 0) {
        wx.showToast({
          title: `请选择至少一项进行投票`,
          icon: 'none',
        });
        return;
      }
    }
    let arr = [];
    for (let i = 0, len = optionid.length; i < len; i++) {
      if (optionid[i]) {
        arr.push(optionid[i]);
      }
    }
    optionid = arr.join(',');
    $util.request({
      url: 'entry/wxapp/partyman.vote.voteAction',
      data: {
        voteid: id,
        optionid: optionid,
      },
      success: (res) => {
        wx.showToast({
          title: `投票成功`,
          icon: 'success',
          success: () => {
            if (isresult == 1) {
              setTimeout(() => {
                this.getVoteInfo(id);
              }, 1000);
            } else {
              wx.navigateBack({
                delta: 1,
              });
            }
          },
        });
      },
    });
  },
  checkboxChange(e) {
    const {
      info,
      option,
    } = this.data;
    let {
      optionid,
      selectnum,
    } = this.data;
    const {
      idx,
    } = e.currentTarget.dataset;
    let isactive = option[idx].isactive;
    if (Number(info.timeexpired) === 1) { return wx.showToast({
      title: '投票未开始',
      icon: 'none',
    }); }
    if (Number(info.timeexpired) === 3) { return wx.showToast({
      title: '投票已结束',
      icon: 'none',
    }); }
    if (Number(info.hasvote) !== 0) return;
    if (isactive) {
      option[idx].isactive = undefined;
      optionid[idx] = '';
      selectnum = selectnum - 1;
    } else {
      if (Number(info.maxselect) === 1) {
        // 单选
        for (let i in option) {
          if (Number(idx) === Number(i)) {
            option[i].isactive = 1;
            optionid[i] = option[i].id;
            selectnum = 1;
          } else {
            option[i].isactive = undefined;
            optionid[i] = '';
          }
        }
      } else {
        // 多选
        if (Number(info.maxselect) !== 0 && Number(info.maxselect) === Number(selectnum)) {
          wx.showToast({
            title: `最多选择${info.maxselect}项`,
            icon: 'none',
          });
          return;
        }
        option[idx].isactive = 1;
        optionid[idx] = option[idx].id;
        selectnum = selectnum + 1;
      }
    }
    this.setData({
      optionid,
      selectnum,
      option,
    });
  },
});